<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        function getNumber() {
            var p = new Promise(function (resolve, reject) {
                //做一些异步操作
                setTimeout(function () {
                    var num = Math.ceil(Math.random() * 10); //生成1-10的随机数
                    if (num <= 5) {
                        resolve(num);
                    }
                    else {
                        reject('数字太大了');
                    }
                }, 2000);
            });
            return p;
        }

        /**
         * 这是一种写法 推荐写法
         */

        getNumber().then(function (data) {
            console.log('===11111==' + data)
        }).catch(function (error) {
            console.log('===22222==' + error)

        })

        /**
         * 另一种写法
         */
        /*     getNumber()
                 .then(
                     function(data){
                         console.log('resolved');
                         console.log(data);
                     }, function(reason, data){
                         console.log('rejected');
                         console.log(reason);
                     }
                 );*/
    </script>
</head>

<body>


<div class="warp">

</div>
</body>
</html>